$(document).ready(function() {

	getArticleBySectionId();
	getAllSection();
	getserInfoBySession();
	
	//进入文章详情
	$(document).on('click', '.article-list>li', function() {
		var articleId = $(this).attr('data-bind');
		 window.open(getContextPath()+'/article/showarticle?articleId='+articleId);
	});
	
	
	//处理初始搜索
	if(GetQueryString('articleName')!=null){
		var articleName =GetQueryString('articleName') ;
	 	var seachArticleURL =getContextPath() + "/article/getsearcharticlebyarticlename";
	$.post(seachArticleURL, {"articleName": articleName,'currentPage':1,'onePageNum':10}, function(data, textStatus, xhr) {
		console.log(data);
		$('.article-list').html('');
		console.log("success");
		console.log(data);
		var obj=eval("("+data+")"); //转换json
		var temphtml='<li><div class="row"><div class="col-md-4 section"><img alt="" src="" style="width:100px;height:60px"></div><div class="col-md-4 section"><div class="row">aaa</div><div class="row" style="word-break:break-all">bbdbbbsdasdasfdgfsfhfghdffghgfjghsfsdfdasdasfdgbbbsdasdas</div><div class="row">foot</div></div></div></li>'
			var article =obj.article; 
		$.map(article, function(item, index) {
			var articleName = item.articleName.replace(/<[^>]+>/g,"");
			var section = item.section;
			if(articleName.length>16){
	            articleName = articleName.substr(0,16)+'...';
	        }
	        var articleContent = item.articleContent.replace(/<[^>]+>/g,"");
	        if(articleContent.length>60){
	        	articleContent = articleContent.substr(0,60)+'...';
	        }
	        var articleTime =  getLocalTime(item.articleTime);
	        var user = item.user;
			temphtml='<li data-bind="'+item.articleId+'" class="list-group-item"><div class="row"><div class="col-md-3 section"><img alt="" src="/file/image/'+item.articleCover+'" style="width:100px;height:60px"></div><div class="col-md-9 section"><div class="row">'+articleName+'</div><div class="row" style="word-break:break-all">'+articleContent+'</div><div class="row">分类：<span class="label label-primary">'+section.sectionName+'</span>&nbsp;&nbsp;时间:<span class="label label-primary">'+articleTime+'</span>&nbsp;&nbsp;作者:<span class="label label-primary">'+user.userName+'</span></div></div></div></li>'
			$('.article-list').append(temphtml);
		});
		
	}); 
	}

	
	
	/**
	 * 获取用户信息并初始赋值
	 */
	function getserInfoBySession(){
		var getserinfobysessionURL = getContextPath()+"/user/getserinfobysession";
		$.get(getserinfobysessionURL, function(data) {
			console.log(data);
			var obj=eval("("+data+")"); //转换json
			if(obj.sucess == true){
			var user = obj.user;
			$('#user_login').html('<center><a href="/smm3/user/backStag"><img style="width:50px;height:50px;border-radius:50px;" src="/file/image/'+user.userImageUrl+'"><a/></center>')
			}else{
				console.log('获取用户信息失败');
			}
		});
	}


/**
 * 获取所有板块
 */	
	function getAllSection(){
	    var getAllSectionURL = getContextPath()+"/front/getallsection";

	    $.ajax({
	        url: getAllSectionURL,
	        async : false, //设置成为 同步的 ,
	    })
	        .done(function(data) {
	            console.log("success");
	            var obj=eval("("+data+")"); //转换json
	            //alert(obj.sucess==true);
	            if(obj.sucess==true){
	                var section = obj.section;
	                $.map(section, function(item, index) {
	                    if((index+1)%3==0){
	                        var  tempHTML='<a class="btn btn-default" target="_black" href="articlesort.jsp?sectionId='+item.sectionId+'" role="button">'+item.sectionName+'</a><br>';
	                    }else {
	                    var  tempHTML='<a class="btn btn-default" target="_black" href="articlesort.jsp?sectionId='+item.sectionId+'" role="button">'+item.sectionName+'</a>'
	                    }
	                    $('.section').append(tempHTML);
	                });
	            }
	            console.log(data);
	        })
	        .fail(function() {
	            console.log("error");
	        })
	        .always(function() {
	            console.log("complete");
	        });

	}

/**
 * 获取板块文章
 */
function getArticleBySectionId(){
	var getarticlebysectionIdURL= getContextPath()+'/article/getarticlebysectionId';
	$.ajax({
		url: getarticlebysectionIdURL,
		data: {'sectionId':GetQueryString('sectionId'),'currentPage':1,'onePageNum':10 },
	})
	.done(function(data) {
		$('.article-list').html('');
		console.log("success");
		console.log(data);
		var obj=eval("("+data+")"); //转换json
		var temphtml='<li><div class="row"><div class="col-md-4 section"><img alt="" src="" style="width:100px;height:60px"></div><div class="col-md-4 section"><div class="row">aaa</div><div class="row" style="word-break:break-all">bbdbbbsdasdasfdgfsfhfghdffghgfjghsfsdfdasdasfdgbbbsdasdas</div><div class="row">foot</div></div></div></li>'
			var article =obj.article; 
		$.map(article, function(item, index) {
			var articleName = item.articleName.replace(/<[^>]+>/g,"");
			var section = item.section;
			if(articleName.length>16){
                articleName = articleName.substr(0,16)+'...';
            }
            var articleContent = item.articleContent.replace(/<[^>]+>/g,"");
            if(articleContent.length>60){
            	articleContent = articleContent.substr(0,60)+'...';
            }
            var articleTime =  getLocalTime(item.articleTime);
            var user = item.user;
			temphtml='<li data-bind="'+item.articleId+'" class="list-group-item"><div class="row"><div class="col-md-3 section"><img alt="" src="/file/image/'+item.articleCover+'" style="width:100px;height:60px"></div><div class="col-md-9 section"><div class="row">'+articleName+'</div><div class="row" style="word-break:break-all">'+articleContent+'</div><div class="row">分类：<span class="label label-primary">'+section.sectionName+'</span>&nbsp;&nbsp;时间:<span class="label label-primary">'+articleTime+'</span>&nbsp;&nbsp;作者:<span class="label label-primary">'+user.userName+'</span></div></div></div></li>'
			$('.article-list').append(temphtml);
		});
	})
	.fail(function() {
		console.log("error");
	})
	.always(function() {
		console.log("complete");
	});
	
}

/**
 * 获取地址栏信息
 */	
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}	

});


function serachClick(){
	var articleName = $('.search').val();
 	var seachArticleURL =getContextPath() + "/article/getsearcharticlebyarticlename";
$.post(seachArticleURL, {"articleName": articleName,'currentPage':1,'onePageNum':10}, function(data, textStatus, xhr) {
	console.log(data);
	$('.article-list').html('');
	console.log("success");
	console.log(data);
	var obj=eval("("+data+")"); //转换json
	var temphtml='<li><div class="row"><div class="col-md-4 section"><img alt="" src="" style="width:100px;height:60px"></div><div class="col-md-4 section"><div class="row">aaa</div><div class="row" style="word-break:break-all">bbdbbbsdasdasfdgfsfhfghdffghgfjghsfsdfdasdasfdgbbbsdasdas</div><div class="row">foot</div></div></div></li>'
		var article =obj.article; 
	$.map(article, function(item, index) {
		var articleName = item.articleName.replace(/<[^>]+>/g,"");
		var section = item.section;
		if(articleName.length>16){
            articleName = articleName.substr(0,16)+'...';
        }
        var articleContent = item.articleContent.replace(/<[^>]+>/g,"");
        if(articleContent.length>60){
        	articleContent = articleContent.substr(0,60)+'...';
        }
        var articleTime =  getLocalTime(item.articleTime);
        var user = item.user;
		temphtml='<li data-bind="'+item.articleId+'"><div class="row"><div class="col-md-3 section"><img alt="" src="/file/image/'+item.articleCover+'" style="width:100px;height:60px"></div><div class="col-md-9 section"><div class="row">'+articleName+'</div><div class="row" style="word-break:break-all">'+articleContent+'</div><div class="row">分类：<span class="label label-primary">'+section.sectionName+'</span>&nbsp;&nbsp;时间:<span class="label label-primary">'+articleTime+'</span>&nbsp;&nbsp;作者:<span class="label label-primary">'+user.userName+'</span></div></div></div></li>'
		$('.article-list').append(temphtml);
	});
	
}); 
}

function getContextPath() { //获取根路径

	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0, index + 1);
	return result;
}


/*
 * java Date格式 转化
 */
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}

function getLocalTime(nS) {  
	var crtTime = new Date(nS);
	 return top.dateFtt("yyyy-MM-dd",crtTime);
}